package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.impl.ProductServiceImpl;
import com.atguigu.spzx.manager.entity.Product;
import com.atguigu.spzx.common.result.Result;
import com.atguigu.spzx.manager.vo.product.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * ProductController
 * </p>
 *
 * @author flyduck
 * @since 2023/10/30
 */
@Api(tags = "商品接口")
@RestController
@RequestMapping("/admin/product/product")
public class ProductController {

    @Autowired
    private ProductServiceImpl productService;

    @ApiOperation("商品上下架")
    @GetMapping("/updateStatusById/{id}/{status}")
    public Result updateStatusById(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatusById(id, status);
        return Result.ok();
    }

    @ApiOperation("商品审核")
    @GetMapping("/updateAuditStatusById/{id}/{auditStatus}")
    public Result updateAuditStatusById(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatusById(id, auditStatus);
        return Result.ok();
    }

    @ApiOperation("根据id删除商品")
    @DeleteMapping("/deleteProductById/{id}")
    public Result deleteProductById(@PathVariable Long id){
        productService.deleteProductById(id);
        return Result.ok();
    }

    @ApiOperation("修改商品")
    @PostMapping("/updateProductById")
    public Result updateProductById(@RequestBody ProductUpdateReqVo reqVo){
        productService.updateProductById(reqVo);
        return Result.ok();
    }

    @ApiOperation("根据id查询商品详情")
    @GetMapping("/getProductById/{id}")
    public Result getById(@PathVariable Long id){
        ProductDetailsRespVo respVo = productService.getProductById(id);
        return Result.ok(respVo);
    }

    @ApiOperation("添加商品")
    @PostMapping("/saveProduct")
    public Result saveProduct(@RequestBody ProductSaveReqVo reqVo){
        productService.saveProduct(reqVo);
        return Result.ok();
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/getProductPageByQuery/{page}/{limit}")
    public Result getProductPageByQuery(@PathVariable Integer page,
                              @PathVariable Integer limit,
                              ProductQueryReqVo reqVo){
        Page<Product> pageParam = new Page<>(page, limit);
        Page<ProductQueryRespVo> pageModel = productService.getProductPageByQuery(pageParam,reqVo);
        return Result.ok(pageModel);
    }
}
